﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using WarehouseManagement.BusinessLogic.BC;
using WarehouseManagement.BusinessLogic.DTO;

namespace WarehouseManagement.Views.PO
{
    public partial class ViewReceipts : DevExpress.XtraEditors.XtraForm
    {
        private ReceiptBC _receiptBC;

        public ViewReceipts()
        {
            InitializeComponent();
            _receiptBC = new ReceiptBC();
        }

        private void ViewReceipts_Load(object sender, EventArgs e)
        {
            txtFromDate.Properties.MaxValue = DateTime.Now.Date;
            cboOption_SelectedIndexChanged(sender, e);
            //PrepareCellMergeSettings();
        }
        private void txtToDate_EditValueChanged(object sender, EventArgs e)
        {
            if (FromDate > ToDate)
                FromDate = ToDate;

        }

        private void txtFromDate_EditValueChanged(object sender, EventArgs e)
        {
            if (FromDate > ToDate)
                ToDate = FromDate;
            //_presenter.LoadImportList();
        }
        private void cboOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            AssignDate();
        }

        private void AssignDate()
        {
            var today = DateTime.Now;
            switch (cboOption.SelectedIndex)
            {
                case 1:
                    {
                        DateTime startOfWeek = DateTime.Today.AddDays(-1 * (int)(DateTime.Today.DayOfWeek));
                        this.FromDate = startOfWeek;
                        this.ToDate = startOfWeek.AddDays(6);
                        break;
                    }
                case 0:
                    {
                        this.FromDate = today;
                        this.ToDate = FromDate;
                        break;
                    }
                case 2:
                    {
                        this.FromDate = new DateTime(today.Year, today.Month, 1);
                        this.ToDate = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
                        break;
                    }
                case 3:
                    {
                        var period = GetPeriod(today.Month);
                        this.FromDate = GetStartDayOfPeriod(period, today.Year);
                        var todate = this.FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 4:
                    {
                        this.FromDate = new DateTime(today.Year, 1, 1);
                        this.ToDate = new DateTime(today.Year, 12, DateTime.DaysInMonth(today.Year, 12));
                        break;
                    }

                case 5:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 1, 1);
                        this.ToDate = new DateTime(today.Year, 1, DateTime.DaysInMonth(today.Year, 1));
                        break;
                    }
                case 6:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 2, 1);
                        this.ToDate = new DateTime(today.Year, 2, DateTime.DaysInMonth(today.Year, 2));
                        break;
                    }
                case 7:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 3, 1);
                        this.ToDate = new DateTime(today.Year, 3, DateTime.DaysInMonth(today.Year, 3));
                        break;
                    }
                case 8:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 4, 1);
                        this.ToDate = new DateTime(today.Year, 4, DateTime.DaysInMonth(today.Year, 4));
                        break;
                    }
                case 9:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 5, 1);
                        this.ToDate = new DateTime(today.Year, 5, DateTime.DaysInMonth(today.Year, 5));
                        break;
                    }
                case 10:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 6, 1);
                        this.ToDate = new DateTime(today.Year, 6, DateTime.DaysInMonth(today.Year, 6));
                        break;
                    }
                case 11:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 7, 1);
                        this.ToDate = new DateTime(today.Year, 7, DateTime.DaysInMonth(today.Year, 7));
                        break;
                    }
                case 12:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 8, 1);
                        this.ToDate = new DateTime(today.Year, 8, DateTime.DaysInMonth(today.Year, 8));
                        break;
                    }
                case 13:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 9, 1);
                        this.ToDate = new DateTime(today.Year, 9, DateTime.DaysInMonth(today.Year, 9));
                        break;
                    }
                case 14:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 10, 1);
                        this.ToDate = new DateTime(today.Year, 10, DateTime.DaysInMonth(today.Year, 10));
                        break;
                    }
                case 15:
                    {
                        // Thang 11
                        this.FromDate = new DateTime(today.Year, 11, 1);
                        this.ToDate = new DateTime(today.Year, 11, DateTime.DaysInMonth(today.Year, 11));
                        break;
                    }
                case 16:
                    {
                        // Thang 12
                        this.FromDate = new DateTime(today.Year, 12, 1);
                        this.ToDate = new DateTime(today.Year, 12, DateTime.DaysInMonth(today.Year, 12));
                        break;
                    }

                case 17:
                    {
                        // Quy 1
                        this.FromDate = GetStartDayOfPeriod(1, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 18:
                    {
                        // Quy 2
                        this.FromDate = GetStartDayOfPeriod(2, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 19:
                    {
                        // Quy 3
                        this.FromDate = GetStartDayOfPeriod(3, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 20:
                    {
                        // Quy 4
                        this.FromDate = GetStartDayOfPeriod(4, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
            }
        }

        private int GetPeriod(int month)
        {
            if (month >= 1 && month <= 3)
                return 1;
            else if (month >= 4 && month <= 6)
                return 2;
            else if (month >= 7 && month <= 9)
                return 3;
            else
                return 4;
        }

        private DateTime GetStartDayOfPeriod(int period, int year)
        {
            if (period < 1 || period > 4)
                throw new ArgumentException("Period invalid");
            return new DateTime(year, (period * 3) - 2, 1);
        }

        public DateTime FromDate
        {
            get
            {
                return txtFromDate.DateTime;
            }
            set
            {
                txtFromDate.DateTime = value;
            }
        }

        public DateTime ToDate
        {
            get
            {
                return txtToDate.DateTime;
            }
            set
            {
                txtToDate.DateTime = value;
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            var list = _receiptBC.GetReceipts(FromDate, ToDate);
            grdImportList.DataSource = list;
            SplashScreenManager.CloseForm();
        }

        private void grvImportList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var focusObj = GetFocusObject();
            btnDelete.Enabled = focusObj != null;
        }

        private ReceiptDTO GetFocusObject()
        {
            return grvImportList.GetFocusedRow() as ReceiptDTO;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có chắc muốn xóa phiếu thu này?", "Xóa phiếu thu", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            var focusObj = GetFocusObject();
            try
            {
                _receiptBC.DeleteReceipt(focusObj.Id);
                btnView_Click(this, e);
            }
            catch
            {
                XtraMessageBox.Show("Không thể xóa phiếu thu. Xin vui lòng thử lại sau", "Không thể xóa phiếu thu", MessageBoxButtons.OK
                    , MessageBoxIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}